1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import junit.framework.TestCase;
22  
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.NoSuchElementException;
26  
27  /**
28   * Unit test for {@link MinimalIterable}.
29   *
30   * @author Kevin Bourrillion
31   */
32  @GwtCompatible
33  public class MinimalIterableTest extends TestCase {
34  
35    public void testOf_empty() {
36      Iterable<String> iterable = MinimalIterable.<String>of();
37      Iterator<String> iterator = iterable.iterator();
38      assertFalse(iterator.hasNext());
39      try {
40        iterator.next();
41        fail();
42      } catch (NoSuchElementException expected) {
43      }
44      try {
45        iterable.iterator();
46        fail();
47      } catch (IllegalStateException expected) {
48      }
49    }
50  
51    public void testOf_one() {
52      Iterable<String> iterable = MinimalIterable.of("a");
53      Iterator<String> iterator = iterable.iterator();
54      assertTrue(iterator.hasNext());
55      assertEquals("a", iterator.next());
56      assertFalse(iterator.hasNext());
57      try {
58        iterator.next();
59        fail();
60      } catch (NoSuchElementException expected) {
61      }
62      try {
63        iterable.iterator();
64        fail();
65      } catch (IllegalStateException expected) {
66      }
67    }
68  
69    public void testFrom_empty() {
70      Iterable<String> iterable
71          = MinimalIterable.from(Collections.<String>emptySet());
72      Iterator<String> iterator = iterable.iterator();
73      assertFalse(iterator.hasNext());
74      try {
75        iterator.next();
76        fail();
77      } catch (NoSuchElementException expected) {
78      }
79      try {
80        iterable.iterator();
81        fail();
82      } catch (IllegalStateException expected) {
83      }
84    }
85  
86    public void testFrom_one() {
87      Iterable<String> iterable
88          = MinimalIterable.from(Collections.singleton("a"));
89      Iterator<String> iterator = iterable.iterator();
90      assertTrue(iterator.hasNext());
91      assertEquals("a", iterator.next());
92      try {
93        iterator.remove();
94        fail();
95      } catch (UnsupportedOperationException expected) {
96      }
97      assertFalse(iterator.hasNext());
98      try {
99        iterator.next();
100       fail();
101     } catch (NoSuchElementException expected) {
102     }
103     try {
104       iterable.iterator();
105       fail();
106     } catch (IllegalStateException expected) {
107     }
108   }
109 }